﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jumpton.Persistent;
using System.Data;
using Jumpton.Data;
using Jumpton.SFA.Server.Entity;

namespace Jumpton.SFA.Server.BusinessRule.InStoreDataMeasures
{
    public class MeasuresGroupItemDetailManager : BusinessLogic
    {
        public MEASURE_ITEM_DETAIL Load(int ID)
        {
            return (MEASURE_ITEM_DETAIL)ObjectRow.Load(typeof(MEASURE_ITEM_DETAIL), string.Format("this.ID={0}", ID));
        }

        //绑定判断是否有效
        public ObjectTable GetActiveYes(int Valid, decimal domainid)
        {
            StringBuilder sb = new StringBuilder();
            if(Valid >= 0)
            {
                sb.AppendFormat("this.ACTIVE='{0}'", Valid);
            }
            if(domainid > 0)
            {
                sb.AppendFormat("and this.DOMAINID='{0}'", domainid);
            }
            return new ObjectTable(typeof(MEASURE_ITEM_DETAIL), sb.ToString().TrimStart('a', 'n', 'd'));
        }

        public ObjectTable GetMeasuresGroupItemDetailTable(decimal domainid, string active)
        {
            string strValid = string.Empty;
            if(!string.IsNullOrEmpty(active))
                strValid = " and ACTIVE='" + active + "'";
            return new ObjectTable(typeof(MEASURE_ITEM_DETAIL), "this.DOMAINID='" + domainid + "'" + strValid);

        }

        public MEASURE_ITEM_DETAIL Load(decimal domainID, string code)
        {
            return (MEASURE_ITEM_DETAIL)ObjectRow.Load(typeof(MEASURE_ITEM_DETAIL), string.Format("this.DOMAINID='{0}' and this.CODE='{1}'", domainID, code));
        }




        public bool Save(MEASURE_ITEM_DETAIL measureitemdetail)
        {
            try
            {

                if(measureitemdetail.State == DataRowState.Added)
                {
                    measureitemdetail.CREATETIME = long.Parse(BusinessLogic.GetDateTimeString(DateTime.Now));
                }
                measureitemdetail.Save();

                return true;
            }
            catch(Exception ex)
            {
                measureitemdetail.Connection.RollbackTransaction();
                this.message = ex.Message;
                throw ex;
            }
        }

        public ObjectTable GetMeasureGroupItemDetailList(string domainID, string updatetime)
        {
            return new ObjectTable(typeof(MEASURE_ITEM_DETAIL), "this.DOMAINID=" + domainID + " and this.LAST_UPDATE_TIME>'" + updatetime + "'");
        }

        public DataSet GetMeasureItemDetailList(string domainID, string updatetime)
        {
            string strSql = "SELECT * FROM  MEASURE_ITEM_DETAIL a,  MEASURE_DETAIL b   WHERE a.MEASUER_DETAIL_ID=b.ID and b.DOMAIN_ID={0} and b.LAST_UPDATE_TIME>'{1}'"
                 + " Order By a.[SEQUENCE];";

            strSql = string.Format(strSql, domainID, updatetime);
            DataSet ds = SqlData.OpenSql(strSql);
            return ds;
        }

        public DataSet GetMeasureGroupItemDetailTable(decimal domainID, string itemId)
        {
            string strSql = " SELECT * FROM MEASURE_DETAIL WHERE DOMAIN_ID={0} AND ID IN (SELECT MEASUER_DETAIL_ID FROM MEASURE_ITEM_DETAIL WHERE MEASUER_ITEMS_ID={1} AND DOMAIN_ID={2})";
            strSql = string.Format(strSql, domainID, itemId, domainID);
            DataSet ds = SqlData.OpenSql(strSql);
            return ds;
        }

        public string getInputType(string v)
        {
            switch(v)
            {
                case "1":
                    return "复选框";
                case "2":
                    return "整数输入框";
                case "3":
                    return "小数输入框";
                case "4":
                    return "文本输入框";
                case "5":
                    return "单选框";
                case "6":
                    return "拍照";
                case "7":
                    return "日期";
            }
            return "未知";
        }



    }
}
